const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        selected1: true,
        selected2: false,
        // 上传图片
        showInput: true,
        img_arr: [],
        tempFiles: {},
        formdata: '',
        fileIds: [],
        message: null,
        subminted:false,
        //模拟数据
        cateItems: [{
                cate_id: 1,
                ishaveChild: true,
                // selected1: true,

            },
            {
                cate_id: 2,
                ishaveChild: true,

            },
        ],
        curNav: 1,
        curIndex: 0
    },
    bindTextInput(e) {
        this.setData({
            message: e.detail.value
        })
    },
    chooseImg(e) {
        var that = this;
        if (this.data.img_arr && this.data.img_arr.length==3){
            wx.showModal({
                title: '提示',
                content: '最多上传三张图片',
                showCancel: false,
            })
            return;
        }
        wx.chooseImage({
            count:3,
            success: function(res) {
                console.log(res);
                if (res.errMsg == 'chooseImage:ok') {
                    let tempFilePaths = res.tempFilePaths;
                    let tempFiles = res.tempFiles;
                    that.setData({
                        img_arr: tempFilePaths,
                        tempFiles: tempFiles
                    })
                }
            },
        })
    },
    // 图片上传函数
    submit: function(e) {
        let that = this;
        if (that.data.subminted == true){
            return;
        }
        if (!this.data.message || this.data.message.length<10){
            wx.showModal({
                title: '提示',
                content: '反馈内容必须大于或等于10个字符',
                showCancel:false
            })
            return;
        }
        that.data.subminted = true;
        wx.showModal({
            title: '提示',
            content: '确定提交?',
            success: res => {
                if (res.confirm) {
                    if (that.data.img_arr && that.data.img_arr.length>0){
                        that.upload();
                    }else{
                        that.submitFeedback(null);
                    }
                }else{
                    that.data.subminted = false;
                }
            }
        })
    },
    upload: function() {
        let that = this
        let fileIds = [];
        for (let i in this.data.img_arr) {
            let tempFilePath = this.data.img_arr[i];
            let fileName = tempFilePath.substring(tempFilePath.lastIndexOf(".")-32, tempFilePath.lastIndexOf("."));
            console.log('fileName',fileName);
            wx.uploadFile({
                url: app.globalData.url + '/doUpload', //仅为示例，非真实的接口地址
                filePath: tempFilePath,
                name: 'mFile',
                formData: {
                    'title': "feedback_"+fileName
                },
                header: {
                    'token': wx.getStorageSync('token'),
                    'loginId': app.globalData.user.uuId
                },
                success: function(res) {
                    let data ;
                    console.log(res, typeof res.data)
                    if (typeof res.data == 'string') {
                        data = JSON.parse(res.data);
                    }else{
                        data = data.data;
                    }
                    if (data.code == 0) {
                        that.data.subminted = false;
                        let fileId=data.data;
                        fileIds.push(fileId);
                        that.setData({
                            fileIds: fileIds
                        })
                        if (i == that.data.img_arr.length - 1) {
                            that.submitFeedback(fileIds);
                        }
                    } else {
                        wx.showToast({
                            title: '文件上传失败,请稍后再试.',
                        })
                    }
                }
            })
        }
    },
    submitFeedback(fileIds){
        let that = this;
        let url = '/user/feedback/addFeedback';
        let param = {
            userId: app.globalData.user.id,
            message: that.data.message,
            photo: fileIds?fileIds.join(",") + "":""
        }
        app.post(url, param, res => {
            if (res.code == 0) {
                wx.showToast({
                    title: '提交成功!',
                    icon: 'success',
                    duration: 2000,
                })
                that.setData({
                    dataList: null,
                    showInput: false
                })
                that.tapListPage();
            } else {
                wx.showToast({
                    title: '提交失败!服务器繁忙.',
                    duration: 2000,
                })
            }
        })
    },
    upimg: function() {
        var that = this;
        if (this.data.img_arr.length < 3) {
            wx.chooseImage({
                sizeType: ['original', 'compressed'],
                success: function(res) {
                    that.setData({
                        img_arr: that.data.img_arr.concat(res.tempFilePaths)
                    })
                }
            })
        } else {
            wx.showToast({
                title: '最多上传三张图片',
                icon: 'loading',
                duration: 3000
            });
        }
    },
    tapFormPage: function(e) {
        console.log(e);
        if (e) {}
        let showInput = this.data.showInput;
        this.setData({
            showInput: true,
        })
    },
    tapListPage: function(e) {
        if(!this.data.dataList){
            let that = this;
            let url = '/user/feedback/findFeedbackList';
            let param = {
                userId: app.globalData.user.id
            }
            app.post(url, param, res => {
                console.log(res);
                if (res.code == 0) {
                    that.setData({
                        dataList: res.data
                    })
                }
            })
        }
        this.setData({
            showInput: false,
        })
    },
})